package action.FrontAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.CroBuycars;
import entity.CroCustomer;
import entity.CroPurchase;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroBuycarsService;
import service.CroPurchaseService;
import vo.BuyCargoodsVo;
import vo.GoodsPropertiesNameVo;
import vo.ParseBuycarVo;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuweijie on 2017/5/19.
 */
public class BuyCarAction extends ActionSupport {

    @Autowired
    private CroBuycarsService croBuycarsService;
    @Autowired
    private CroPurchaseService croPurchaseService;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 5;
    private List<CroBuycars> croBuycarlist;
    private List<BuyCargoodsVo> goodsdetails=new ArrayList<>();
    private CroPurchase croPurchase;
    private String buycarid;
    private String purchaseid;
    private String del_flag;
    private String numbers;
    private String successResult;
    private double numprice;//当前购物车物品的总价

    /*
    * 分页显示购物车
    * */
    public String buycarlist(){
        Pagination<CroBuycars> page;
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer=(CroCustomer) context.getAttribute("customer");
        croPurchase=croPurchaseService.FindPurchaseByCus(customer.getId());
        if(pageIndex==0){
            page=croBuycarsService.FindByPurchase(croPurchase,1,pageSize);
        }else {
            page=croBuycarsService.FindByPurchase(croPurchase,pageIndex,pageSize);
        }
        List<CroBuycars> buycars=croBuycarsService.listByPurchase(croPurchase);
        for(CroBuycars buycar:buycars){
            numprice+=buycar.getSumprice();
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setCroBuycarlist(page.getItems());
        for(CroBuycars croBuycars:croBuycarlist){
            BuyCargoodsVo buyCargoodsVo=new BuyCargoodsVo();
            ParseBuycarVo parseBuycarVo= JSON.parseObject(croBuycars.getGoodsdetail(),ParseBuycarVo.class);
            buyCargoodsVo.setId(croBuycars.getId());
            buyCargoodsVo.setName(parseBuycarVo.getTitle());
            StringBuffer sb=new StringBuffer();
            for(GoodsPropertiesNameVo goodsdetail:parseBuycarVo.getPropertiesName()){
                sb.append(goodsdetail.getPropName()+":"+goodsdetail.getPropValueName()+",  ");
            }
            buyCargoodsVo.setDescrib(sb.toString());
            buyCargoodsVo.setPhoto(parseBuycarVo.getPicUrl());
            buyCargoodsVo.setPrice(parseBuycarVo.getPrice());
            buyCargoodsVo.setActprice(croBuycars.getActualprice());
            buyCargoodsVo.setNumbers(croBuycars.getNumbers());
            buyCargoodsVo.setSumprice(croBuycars.getSumprice());
            goodsdetails.add(buyCargoodsVo);
        }
        return SUCCESS;
    }

    /*
   * 删除购物车内商品
   * */
    public String deletebuycar(){
        if(croBuycarsService.DeleteBuyCarById(buycarid,del_flag)){
            successResult="0";
        }else {
            successResult="1";
        }
        return SUCCESS;
    }

    /**
     * 更新购物车商品信息
     * */
    public String updatebuycar(){
        if(croBuycarsService.UpdateBuyCarById(buycarid,numbers)){
            successResult="0";
        }else {
            successResult="1";
        }
        return SUCCESS;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<CroBuycars> getCroBuycarlist() {
        return croBuycarlist;
    }

    public void setCroBuycarlist(List<CroBuycars> croBuycarlist) {
        this.croBuycarlist = croBuycarlist;
    }

    public List<BuyCargoodsVo> getGoodsdetails() {
        return goodsdetails;
    }

    public void setGoodsdetails(List<BuyCargoodsVo> goodsdetails) {
        this.goodsdetails = goodsdetails;
    }

    public CroPurchase getCroPurchase() {
        return croPurchase;
    }

    public void setCroPurchase(CroPurchase croPurchase) {
        this.croPurchase = croPurchase;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public String getBuycarid() {
        return buycarid;
    }

    public void setBuycarid(String buycarid) {
        this.buycarid = buycarid;
    }

    public String getDel_flag() {
        return del_flag;
    }

    public void setDel_flag(String del_flag) {
        this.del_flag = del_flag;
    }

    public String getNumbers() {
        return numbers;
    }

    public void setNumbers(String numbers) {
        this.numbers = numbers;
    }

    public double getNumprice() {
        return numprice;
    }

    public void setNumprice(double numprice) {
        this.numprice = numprice;
    }

    public String getPurchaseid() {
        return purchaseid;
    }

    public void setPurchaseid(String purchaseid) {
        this.purchaseid = purchaseid;
    }
}
